package admin;

import inc.CommonFunction;
import inc.Parameters;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletResponse;

import lottery.BetWay;
import lottery.Lottery;
import lottery.OpenReward;

import org.apache.struts2.interceptor.ServletResponseAware;

import reflect.ReflectObject;

import com.opensymphony.xwork2.ActionSupport;

import dao.Hdao;

public class LotteryAction extends ActionSupport implements ServletResponseAware
{
    private CommonFunction fun = new CommonFunction();
    private HttpServletResponse resp; 
    
    private String lotteryName;
    private String timespace;
    private String beginTime;
    private String endTime;
    //betway
    private String betwayName;
    private int lotteryId;
    private int fatherId;
    private int bitCount;
    private String bitStr;
    //open reward
    private String clock1; //要添加的时间段1,并为第一个开奖正点时间
    private String clock2;
    private int spaceTime;  //间隔时间（分）,并以开奖时间为准
	private int days;  //天数
    private String spbetBeginTime;
    private String spbetEndTime;
    private String spopenRewardTime; //0
    private String startDate; //起始日期
    private String numberDemo;  //该时间段第一期期号(不带日期)
    private int numberRule; //期号命名规则0-日期 1-累加
    private int totalOpen;  //每天总期数
    
   /* public static void main(String args[]){
        String str = "01:01:30";
        System.out.println(getSeconds(str));
    }*/
    
    
	//从05:23:38的字符串得到秒数
    int getSeconds(String timeStr){
        if(timeStr.indexOf(":") != timeStr.lastIndexOf(":")){
            //有两个冒号即时：分：秒
            return Integer.parseInt(timeStr.split(":")[0])*60*60+Integer.parseInt(timeStr.split(":")[1])*60+Integer.parseInt(timeStr.split(":")[2]);
        }else{
            //时：分
            return Integer.parseInt(timeStr.split(":")[0])*60*60+Integer.parseInt(timeStr.split(":")[1])*60;
        }
    }
    
    //add open reward
    public String addOpenReward(){
    	String results = "";
    	String startDateArr[]  =startDate.split("-");
    	int extNumber;  //期号后缀
    	String extNumberStr = "";
    	int year = 0;
    	int month = 0;
    	int day = 0;
        int day0 = 0;//保留最初传进来的day
    	if(startDateArr.length==3){
	    	year = Integer.parseInt(startDateArr[0]);
	    	month = Integer.parseInt(startDateArr[1]);
	    	day = Integer.parseInt(startDateArr[2]);
            day0 = day;
    	}else{
    		results = "起始日期格式不正确:"+startDate;
    		System.out.println(results);
    		return null;
    	}
    	String yearStr = year+"";
    	String monthStr = month<10 ? ("0"+month) : (month+"");
    	String dayStr = day<10 ? ("0"+day) : (day+"");
        int clock1Num = getSeconds(clock1);
        int clock2Num = getSeconds(clock2);
        int count = 0;  //loop count
        while(clock1Num < clock2Num){
        	//每天同一时间循环添加
            day = day0; //新的期号，天数重置!
        	for(int di=0; di<days; di++){
        		OpenReward or = new OpenReward();
            	or.setLotteryId(lotteryId);
            	or.setLotteryName(lotteryName);
            	or.setBeginTime(clock1Num-Integer.parseInt(spbetBeginTime)*60);
            	or.setEndTime(clock1Num-Integer.parseInt(spbetEndTime)*60);
            	or.setOpenTime(clock1Num-Integer.parseInt(spopenRewardTime)*60);
            	//计算期号
            	if(month==2 && day>29){
            		month ++;
            		day = 1;
            	}else if((month==1 || month==3 || month==5|| month==7|| month==8|| month==10|| month==12) 
            				&& day>31){
            		month ++;
            		day = 1;
            	}else if(day>30){
            		month ++;
            		day = 1;
            	}
            	if(month>12){
            		year++;
            		month = 1;
            	}
            	yearStr = year+"";
            	monthStr = month<10 ? ("0"+month) : (month+"");
            	dayStr = day<10 ? ("0"+day) : (day+"");
            	if(numberRule==0){
            		//期号按日期命名,非黑龙江时时彩
            		extNumber = Integer.parseInt(numberDemo) + count;
            		if(extNumber<10){
            			extNumberStr = "00"+extNumber;
            		}else if(extNumber<100){
            			extNumberStr = "0"+extNumber;
            		}
            		or.setNumber(yearStr+monthStr+extNumberStr);
            	}else if(numberRule==1){
            		extNumber = Integer.parseInt(numberDemo) + count + totalOpen;
            		or.setNumber(extNumber+"");
            	}
            	Calendar cal=Calendar.getInstance(); 
            	cal.clear();
            	cal.set(year, month, day);
            	or.setOrderTime(new Date(cal.getTime().getTime()+clock1Num));
                or.setEndTimeDate(new Date(cal.getTime().getTime()+clock1Num-Integer.parseInt(spbetEndTime)*60));
            	or.setStatus("1");
            	or.setYyyyMMdd(yearStr+monthStr+dayStr);
                Hdao.save(or);
            	day++;
        	}
        	clock1Num += spaceTime*60;
        	count ++;
        }
        return null;
    }
    /**
     * 添加彩种
     * @return
     * @author liuding,2012-3-29
     */
    public String addLottery(){
        Lottery lt = new Lottery();
        lt.setName(lotteryName);
       // lt.setCount(60*(lt.getEndTime()-lt.getBeginTime())/lt.getSpaceTime() + 1);
        lt.setStatus(Parameters.STATUS_OK);
        int r = Hdao.save(lt);
        String className = "lottery.Lottery";
        HashMap map = null;
        Iterator keyIterator = null;
        String key = null;
        String results = fun.xmlAppend(null, null, "resp", null);
        ArrayList list = Hdao.query(className, null);
        if(list.size()>0){
            results = fun.xmlAppend(results, "resp", "list", null);
        }
        results = fun.xmlAppend(results, "resp", "size", list.size());
        for(Object obj : list){
            results = fun.xmlAppend(results, "list", "obj", null);
            map = ReflectObject.getFieldValues(obj, className);
            keyIterator = map.keySet().iterator();
            while(keyIterator.hasNext()){
                key = (String) keyIterator.next();
                results = fun.xmlAppend(results, "obj", key, map.get(key));          
            }
        }
        fun.ajaxPrint(resp, "xml", null, results);
 System.out.println("addLottery():"+results);
        return null;
    }
    
    /**添加玩法
     * @return
     * @author liuding,2012-3-30
     */
    public String addBetWay(){
        BetWay way = new BetWay();
        way.setBitCount(bitCount);
        way.setBitStr(bitStr);
        way.setFatherId(fatherId);
        way.setLotteryId(lotteryId);
        way.setName(betwayName);
        way.setNullable("N");
        Hdao.save(way);
        String results = ReflectObject.getXMLFieldValues("lottery.BetWay", null);
        fun.ajaxPrint(resp, "xml", null, results);
        System.out.println("addLottery():"+results);
        return null;
    }

    public String getBeginTime()
    {
        return beginTime;
    }

    public void setBeginTime(String beginTime)
    {
        this.beginTime = beginTime;
    }

    public String getEndTime()
    {
        return endTime;
    }

    public void setEndTime(String endTime)
    {
        this.endTime = endTime;
    }

    public String getLotteryName()
    {
        return lotteryName;
    }

    public void setLotteryName(String lotteryName)
    {
        this.lotteryName = lotteryName;
    }

    public String getTimespace()
    {
        return timespace;
    }

    public void setTimespace(String timespace)
    {
        this.timespace = timespace;
    }

    public void setServletResponse(HttpServletResponse resp)
    {
        // TODO Auto-generated method stub
        this.resp = resp;
    }

    public String getBetwayName()
    {
        return betwayName;
    }

    public void setBetwayName(String betwayName)
    {
        this.betwayName = betwayName;
    }


    public String getBitStr()
    {
        return bitStr;
    }

    public void setBitStr(String bitStr)
    {
        this.bitStr = bitStr;
    }
    public int getLotteryId() {
		return lotteryId;
	}

	public void setLotteryId(int lotteryId) {
		this.lotteryId = lotteryId;
	}

	public int getFatherId() {
		return fatherId;
	}

	public void setFatherId(int fatherId) {
		this.fatherId = fatherId;
	}

	public int getBitCount() {
		return bitCount;
	}

	public void setBitCount(int bitCount) {
		this.bitCount = bitCount;
	}

	public String getClock1() {
		return clock1;
	}

	public void setClock1(String clock1) {
		this.clock1 = clock1;
	}

	public String getClock2() {
		return clock2;
	}

	public void setClock2(String clock2) {
		this.clock2 = clock2;
	}

	public int getSpaceTime() {
		return spaceTime;
	}

	public void setSpaceTime(int spaceTime) {
		this.spaceTime = spaceTime;
	}

	public int getDays() {
		return days;
	}

	public void setDays(int days) {
		this.days = days;
	}

	public String getSpbetBeginTime() {
		return spbetBeginTime;
	}

	public void setSpbetBeginTime(String spbetBeginTime) {
		this.spbetBeginTime = spbetBeginTime;
	}

	public String getSpbetEndTime() {
		return spbetEndTime;
	}

	public void setSpbetEndTime(String spbetEndTime) {
		this.spbetEndTime = spbetEndTime;
	}

	public String getSpopenRewardTime() {
		return spopenRewardTime;
	}

	public void setSpopenRewardTime(String spopenRewardTime) {
		this.spopenRewardTime = spopenRewardTime;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setNumberDemo(String numberDemo) {
		this.numberDemo = numberDemo;
	}

	public String getNumberDemo() {
		return numberDemo;
	}

	public void setNumberRule(int numberRule) {
		this.numberRule = numberRule;
	}

	public int getNumberRule() {
		return numberRule;
	}
	 public int getTotalOpen() {
		return totalOpen;
	}

	public void setTotalOpen(int totalOpen) {
		this.totalOpen = totalOpen;
	}
}
